{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用内建函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abs(-3.14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int(5.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int(-5.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hex(25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bin(25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "oct(25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isinstance(3.14, float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isinstance(4, float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in_val = input(\"Give me a number: \")\n",
    "print(\"The number you gave me is \" + str(in_val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "35 in [35, 67]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "35 not in [35, 67] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_mul(x, y):\n",
    "    return (x + 2) * y\n",
    "\n",
    "print(my_mul(3, 4))\n",
    "print(my_mul(x = 3, y = 4))\n",
    "print(my_mul(y = 4, x = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_mul_default(x, y = 2):\n",
    "    return(x + 2)* y\n",
    "print(my_mul_default(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# multiple return value\n",
    "def mul_ret(x, y):\n",
    "    return 3 * x, 4 * y\n",
    "\n",
    "rret = mul_ret(3, 4)\n",
    "print(rret)\n",
    "print(type(rret))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You will see an error\n",
    "rret[0]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fibinacci function\n",
    "# This is not a good example\n",
    "# Do not write code like this when interview\n",
    "def fi(x):\n",
    "    if x in [1, 2]:\n",
    "        return 1\n",
    "    else:\n",
    "        return fi(x-1) + fi(x-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "print(fi(5))\n",
    "print(fi(10))\n",
    "%time print(fi(35))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fi2(n):\n",
    "    if n <= 0:\n",
    "        return 0\n",
    "    elif n <= 2:\n",
    "        return 1\n",
    "    \n",
    "    a, b = 1, 1\n",
    "    for i in range(n-2):\n",
    "        a, b = b, a+b\n",
    "    return b\n",
    "print(fi2(5))\n",
    "print(fi2(10))\n",
    "%time print(fi2(35))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#小作业\n",
    "#写一个函数，求杨辉三角形，第i行，第j列的值\n",
    "#fun(0, 0) = 1\n",
    "#fun(5, 2) = 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#LeetCode\n",
    "#Go and try yourself"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 面向对象与类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Fruit:\n",
    "    def __init__(self, name, price):\n",
    "        self.name = name\n",
    "        self.price = price\n",
    "    \n",
    "    def eatIt(self):\n",
    "        print(\"Give me \" + str(self.price) + \" dollars!\")\n",
    "        print(\"Let's enjoy \" + self.name)\n",
    "        \n",
    "apple = Fruit('apple', 3.5)\n",
    "apple.eatIt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class People:\n",
    "    name = ''\n",
    "    age = 0\n",
    "    \n",
    "    __weight = 0\n",
    "    def __init__(self, n, a, w):\n",
    "        self.name = n\n",
    "        self.age = a\n",
    "        self.__weight = w\n",
    "    \n",
    "    def speak(self):\n",
    "        print(\"%s says: I am %d years old.\" % (self.name, self.age))\n",
    "\n",
    "class Student(People):\n",
    "    'Student class'\n",
    "    grade = ''\n",
    "    def __init__(self, n, a, w, g):\n",
    "        People.__init__(self, n, a, w)\n",
    "        self.grade = g\n",
    "    \n",
    "    def speak(self):\n",
    "        print(\"%s says: I am %d years old. I am in Grade %d .\" \n",
    "              %(self.name, self.age, self.grade))\n",
    "\n",
    "robert = Student('Robert', 16, 180, 9)\n",
    "robert.speak()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "People.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "robert.__module__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(Student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "robert.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Student.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "issubclass(float, object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#小练习\n",
    "#写一个分数类，支持自动约分，并能重打印\n",
    "#a=Frac(2, 6)\n",
    "#print(a)\n",
    "#output: \"Frac <1, 3>\"\n",
    "#Hint: __str__ 方法  __repr__方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "b = 0\n",
    "c = a/b\n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "b = 0\n",
    "try:\n",
    "    c = a / b\n",
    "    print(c)\n",
    "except ZeroDivisionError as e:\n",
    "    print(\"Error: \", e)\n",
    "\n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# practice yourself\n",
    "# raise\n",
    "# finally"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"text.txt\", \"r\")\n",
    "print(f.readlines())\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"text.txt\", \"r\") as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"text_write.txt\", \"a\") as f:\n",
    "    f.write(\"Hello World\")\n",
    "\n",
    "with open(\"text_write.txt\", \"r\") as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list的一些操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(range(0, 10, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis = [2, 3, 5, 7, 11]\n",
    "for index, val in enumerate(lis):\n",
    "    print(index, val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "namelist = [\"Alpha\", \"Bravo\", \"Camp\"]\n",
    "agelist = [35, 46, 78]\n",
    "heightlist = [1.78, 1.35, 1.65]\n",
    "for (name, age, height) in zip(namelist, agelist, heightlist):\n",
    "    print(name, age, height)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成式与生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = [0, 2, 4, 6, 8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = []\n",
    "for val in data1:\n",
    "    data2.append(val * val)\n",
    "print(data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3 = [val * val for val in data1]\n",
    "print(data3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = range(100)\n",
    "data4 = [val * val for val in nums if val % 2 == 0]\n",
    "print(data4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_data = (x * x for x in range(10000))\n",
    "for i in gen_data:\n",
    "    print(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
